import os
import zipfile
from datetime import datetime

def create_release():
    # 获取当前时间作为版本号
    version = datetime.now().strftime("%Y%m%d_%H%M%S")
    release_name = f"package_scanner_v{version}"
    
    # 要包含的文件和目录
    include_files = [
        'app.py',
        'niantie_api.py',
        'config.py',
        'requirements.txt',
        'README.md',
        'schema.sql',
        'run_production.py'
    ]
    
    include_dirs = [
        'static',
        'templates',
        'translations'
    ]
    
    # 要排除的文件类型
    exclude_extensions = {
        '.pyc',
        '.pyo',
        '.pyd',
        '.db',
        '.log'
    }
    
    # 创建zip文件
    zip_filename = f"{release_name}.zip"
    with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
        # 添加单个文件
        for file in include_files:
            if os.path.exists(file):
                print(f"Adding file: {file}")
                zipf.write(file)
        
        # 添加目录
        for dir_name in include_dirs:
            if os.path.exists(dir_name):
                print(f"Adding directory: {dir_name}")
                for root, dirs, files in os.walk(dir_name):
                    for file in files:
                        # 检查文件扩展名
                        if not any(file.endswith(ext) for ext in exclude_extensions):
                            file_path = os.path.join(root, file)
                            print(f"Adding: {file_path}")
                            zipf.write(file_path)
        
        # 创建空的logs目录
        print("Adding empty logs directory")
        zipf.writestr('logs/.gitkeep', '')
    
    print(f"\nRelease package created: {zip_filename}")
    print("\n包含的文件:")
    with zipfile.ZipFile(zip_filename, 'r') as zipf:
        for file in zipf.namelist():
            print(f"- {file}")

if __name__ == '__main__':
    create_release() 